package com.aplink.generic.xmlparser;

import android.content.Context;

import com.aplink.generic.constant.ConfigParameter;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;

public class BehaviorFormatParser {

    private static String getTextNodeValue( Node node ) {
        Node child;
        if ( node != null ) {
            if ( node.hasChildNodes() ) {
                child = node.getFirstChild();
                while ( child != null ) {
                    if ( child.getNodeType() == Node.TEXT_NODE ) {
                        return child.getNodeValue();
                    }
                    child = child.getNextSibling();
                }
            }
        }
        return "";
    }

    public static CustomListviewBehavior parse( Document doc, String tagname,
                                                String srcName ) {
        final CustomListviewBehavior listviewBehavior = new CustomListviewBehavior();
        doc.getDocumentElement().normalize();

        final NodeList nList = doc.getElementsByTagName( tagname );

        if ( nList.getLength() == 0 ) {
            return null;
        }

        final Node nNode = nList.item( 0 );
        if ( nNode.hasAttributes() ) {
            final Attr attr = (Attr) nNode.getAttributes().getNamedItem(
                    ConfigParameter.WIDGET_CONTROL_ID );
            if ( attr == null ) {
                return null; // there is no OBJECT_CLASS ID of listview
            }
            if ( attr != null ) {
                final String ObjectID = attr.getNodeValue();
                if ( ObjectID.compareTo( srcName ) != 0 ) {
                    return null; // since this Object_class didn't match
                    // with srcName
                }
            }
        }

        if ( nNode.getNodeType() == Node.ELEMENT_NODE ) {
            final Element element = (Element) nNode;

            // set action of this control
            NodeList nodes = element
                    .getElementsByTagName( ConfigParameter.BEHAVIOR_ACTION );
            if ( nodes.getLength() != 0 ) {
                final Node nActionNode = nodes.item( 0 );

                if ( nActionNode.hasAttributes() ) {

                    // find attr onItemClickListener
                    Attr attr = (Attr) nActionNode.getAttributes()
                            .getNamedItem( ConfigParameter.BEHAVIOR_ITEMCLICK );
                    if ( attr != null ) {
                        final String ObjectID = attr.getNodeValue();
                        if ( ObjectID.compareTo( ConfigParameter.WIDGET_ACTIVE ) == 0 ) {
                            listviewBehavior.setItemClickListener( true );
                        } // set for Behavior item click
                        else {
                            listviewBehavior.setItemClickListener( false );
                        }
                    }

                    // find attr onItemLongClickListener
                    attr = (Attr) nActionNode.getAttributes().getNamedItem(
                            ConfigParameter.BEHAVIOR_ITEMLONGCLICK );
                    if ( attr != null ) {
                        final String ObjectID = attr.getNodeValue();
                        if ( ObjectID.compareTo( ConfigParameter.WIDGET_ACTIVE ) == 0 ) {
                            listviewBehavior.setItemLongClickListener( true );
                        } // set for Behavior item long click
                        else {
                            listviewBehavior.setItemLongClickListener( false );
                        }
                    }
                    // find attr onSwipeDetector
                    attr = (Attr) nActionNode.getAttributes().getNamedItem(
                            ConfigParameter.BEHAVIOR_SWIPEDETECTOR );
                    if ( attr != null ) {
                        final String ObjectID = attr.getNodeValue();
                        if ( ObjectID.compareTo( ConfigParameter.WIDGET_ACTIVE ) == 0 ) {
                            listviewBehavior.setSwipDetector( true );
                        } // set for Behavior swipe
                        else {
                            listviewBehavior.setSwipDetector( false );
                        }
                    }

                    // find attr onDeleteItem
                    attr = (Attr) nActionNode.getAttributes().getNamedItem(
                            ConfigParameter.BEHAVIOR_DELETEITEM );
                    if ( attr != null ) {
                        final String ObjectID = attr.getNodeValue();
                        if ( ObjectID.compareTo( ConfigParameter.WIDGET_ACTIVE ) == 0 ) {
                            listviewBehavior.setDeleteItem( true );
                        } // set for Behavior item click to delete
                        else {
                            listviewBehavior.setDeleteItem( false );
                        }
                    }
                }
            }

            // set activity for result
            nodes = element
                    .getElementsByTagName( ConfigParameter.BEHAVIOR_ACTIVITYRESULT );
            if ( nodes.getLength() != 0 ) {
                final Node nActivityResultNode = nodes.item( 0 );

                if ( nActivityResultNode.hasAttributes() ) {

                    // set for attribute moveTo="Y"
                    Attr attr = (Attr) nActivityResultNode.getAttributes()
                            .getNamedItem( ConfigParameter.BEHAVIOR_MOVETO );
                    if ( attr == null ) {
                        return listviewBehavior;
                    } // there is no information for Activity for Result;
                    if ( attr != null ) {
                        final String isMoveTo = attr.getNodeValue();
                        if ( isMoveTo.compareTo( ConfigParameter.WIDGET_ACTIVE ) == 0 ) {
                            listviewBehavior.setMoveTo( true );// set Yes
                        } else {
                            return listviewBehavior; // there is no
                            // information for
                            // moveto, thus we
                            // can return
                        }
                    }

                    // set for attribute Activity should be jumped into
                    attr = (Attr) nActivityResultNode.getAttributes()
                            .getNamedItem(
                                    ConfigParameter.BEHAVIOR_MOVETO_ACTIVITY );
                    if ( attr == null ) {
                        return listviewBehavior;
                    } // there is no information for Activity for Result;
                    if ( attr != null ) {
                        final String activityName = attr.getNodeValue();
                        listviewBehavior.setDesiredActivity( activityName );// set
                        // desired
                        // Activity
                    }
                }
                if ( listviewBehavior.getDesiredActivity().compareTo( "" ) != 0 ) {
                    // extract Parameter child tags to pass to new activity
                    // for here
                    if ( nActivityResultNode.getNodeType() == Node.ELEMENT_NODE ) {
                        final Element elementActivityResultNode = (Element) nActivityResultNode;

                        // set parameters
                        final NodeList parameternodes = elementActivityResultNode
                                .getElementsByTagName( ConfigParameter.BEHAVIOR_PARAMETER );
                        if ( parameternodes.getLength() != 0 ) {
                            final ArrayList<String> arrParameter = new ArrayList<String>();
                            String content;
                            for ( int i = 0 ; i < parameternodes.getLength() ; i++ ) {
                                content = getTextNodeValue( parameternodes
                                        .item( i ) );
                                arrParameter.add( content ); // done
                            }
                            listviewBehavior.setPassValueList( arrParameter );
                        }
                    }
                }
            }
        }
        return listviewBehavior;
    }

    public static CustomListviewBehavior parserIntoBehavior(
            Context paramContext, int xmlFileId, String tagname, String srcName ) {
        try {
            final Document document = XMLPaserUtil.openDocumentFrom( paramContext,
                    xmlFileId );
            return parse( document, tagname, srcName );
        } catch ( final ParserConfigurationException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch ( final SAXException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch ( final IOException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static CustomListviewBehavior parserIntoBehavior(
            Context paramContext, String xmlfilename, String tagname,
            String srcName ) {
        try {
            final Document document = XMLPaserUtil.openDocumentFrom( paramContext,
                    xmlfilename );
            return parse( document, tagname, srcName );
        } catch ( final ParserConfigurationException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch ( final SAXException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch ( final IOException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
